package com.test.nnooolwdeaer.view_wooden.widget_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.adapter_wooden.InfoInputListAdapter_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;

import java.util.ArrayList;
import java.util.Arrays;

public class SelectView_wooden extends LinearLayout {

    private Context mContext_wooden;

    private String title_wooden;
    private String hint_wooden;
    private Drawable icon_wooden;

    private TextView titleTv_wooden;
    private ViewGroup clickLayout_wooden;
    private TextView inputTv_wooden;
    private ViewGroup listLayout_wooden;
    private ListView listLv_wooden;

    private OnPickListener_wooden onPickListener_wooden;
    private InterceptPickListener interceptPickListener;//修改选择后的逻辑

    private int itemHeight_wooden = 0;
    private ArrayList<String> items_wooden;
    private InfoInputListAdapter_wooden listAdapter_wooden;

    private final AdapterView.OnItemClickListener onItemClickListener_wooden = (parent, view, position, id) -> {
        if (items_wooden == null) return;
        String pickStr = items_wooden.get(position);

        listLayout_wooden.setVisibility(GONE);

        if (interceptPickListener!=null){
            interceptPickListener.onPick(pickStr);
            return;
        }

        inputTv_wooden.setText(pickStr);

        if (onPickListener_wooden != null) onPickListener_wooden.onPick(pickStr);
    };

    public SelectView_wooden(Context context) {
        super(context);
        init_wooden(context,null);
    }

    public SelectView_wooden(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init_wooden(context,attrs);
    }

    public SelectView_wooden(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init_wooden(context,attrs);
    }

    private void init_wooden(Context context, AttributeSet attrs){

        mContext_wooden = context;

        if (attrs!=null){
            @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SelectView);
            title_wooden = typedArray.getString(R.styleable.SelectView_title);
            hint_wooden = typedArray.getString(R.styleable.SelectView_android_hint);
            icon_wooden = typedArray.getDrawable(R.styleable.SelectView_icon);
        }

        View view = View.inflate(context, R.layout.view_select_wooden,this);
        titleTv_wooden = view.findViewById(R.id.view_select_tv_title);
        clickLayout_wooden = view.findViewById(R.id.view_select_layout_click);
        inputTv_wooden = view.findViewById(R.id.view_select_tv_input);
        listLayout_wooden = view.findViewById(R.id.view_select_layout_list);
        listLv_wooden = view.findViewById(R.id.view_select_lv_list);
        View iconView = view.findViewById(R.id.view_select_layout_icon);
        ImageView iconIv = view.findViewById(R.id.view_select_iv_icon);

        if (TextUtils.isEmpty(title_wooden)) titleTv_wooden.setVisibility(GONE);
        else titleTv_wooden.setText(title_wooden);

        inputTv_wooden.setHint(StringUtil_wooden.getSafeString_wooden(hint_wooden));

        clickLayout_wooden.setOnClickListener(v -> listLayout_wooden.setVisibility(listLayout_wooden.getVisibility()==VISIBLE ? GONE:VISIBLE));

        if (icon_wooden !=null){
            iconView.setVisibility(VISIBLE);
            iconIv.setImageDrawable(icon_wooden);
        }

    }

    public boolean isInputEmpty(){
        return TextUtils.isEmpty(inputTv_wooden.getText());
    }

    public String getInput_wooden(){
        return StringUtil_wooden.getSafeString_wooden(inputTv_wooden.getText().toString());
    }

    public void addListItem_wooden(String... listItems){
        addListItem_wooden(new ArrayList<>(Arrays.asList(listItems)));
    }

    public void addListItem_wooden(ArrayList<String> listItems){
        this.items_wooden = listItems;
        listAdapter_wooden = new InfoInputListAdapter_wooden(mContext_wooden, items_wooden);
        listLv_wooden.setAdapter(listAdapter_wooden);
        listLv_wooden.setOnItemClickListener(onItemClickListener_wooden);

        ViewGroup.LayoutParams layoutParams = listLv_wooden.getLayoutParams();
        if (itemHeight_wooden == 0) itemHeight_wooden = layoutParams.height;
        layoutParams.height = items_wooden.size()* itemHeight_wooden;
        listLv_wooden.setLayoutParams(layoutParams);
    }

    public void setInput_wooden(String text){
        inputTv_wooden.setText(StringUtil_wooden.getSafeString_wooden(text));
    }

    public void setOnPickListener_wooden(OnPickListener_wooden onPickListener_wooden) {
        this.onPickListener_wooden = onPickListener_wooden;
    }

    public void setInterceptPickListener(InterceptPickListener interceptPickListener) {
        this.interceptPickListener = interceptPickListener;
    }

    public interface OnPickListener_wooden {
        void onPick(String pickStr);
    }

    public interface InterceptPickListener{
        void onPick(String pickStr);
    }

}
